/**
 * 
 */
package info.niwota.ziplock;

import info.niwota.ziplock.img.MultiSelectImageActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class ViewsActivity extends Activity {

	private static final int RC_MULTI_SELECT = 1;

	private static final String TAG = "ViewsActivity";

	public static final String EXTRA_VIEW = "extra.view";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "savedInstanceState: " + savedInstanceState);
		}
		super.onCreate(savedInstanceState);
		//
		showView(getIntent());
	}

	private void showView(Intent data) {
		try {
			int view = data.getIntExtra(EXTRA_VIEW, Preferences
					.getDefaultView(this));
			Class<?> clz = ZipFileListActivity.class;

			if (Debug.DEBUG) {
				Log.d(TAG, "view: " + view);
			}
			switch (view) {
			case Preferences.VIEW_LIST:
				clz = ZipFileListActivity.class;
				break;
			case Preferences.VIEW_THUMBNAILS:
				clz = MultiSelectImageActivity.class;
				break;
			default:
			}

			Intent i = new Intent(data.getAction(), data.getData(), this, clz);
			startActivityForResult(i, RC_MULTI_SELECT);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult resultCode " + resultCode + " data: "
					+ data);
		}
		try {
			//
			finishActivity(requestCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (resultCode == RESULT_CANCELED || resultCode == RESULT_OK) {
			doReturn(data, resultCode);
		} else if (resultCode == RESULT_FIRST_USER) {
			showView(data); // switch views
		}
	}

	private void doReturn(Intent data, int rc) {
		setResult(rc, data);
		finish();
	}
}
